from .conn_mysql import ConnectMysql

import datetime

import uuid

class ApiMysqlclass:

    def __init__(self):

        pass

    def get_return(self, token =None):

        con_obj = ConnectMysql()

        if token == None:#检测是否有token值

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg':'请检查所传参数',
                'resultBody':None
            }
        sql = 'select * from token where token=%s' 

        token_info = con_obj.select_all(sql,token)#获取token——info

        if bool(token_info) is False:

            return {

                'status': -1,
                'errorCode': 'keyserror',
                'errorMsg':'无此token对应的数据',
                'resultBody': None,
            }

        else:

            company_id = token_info[0]['company_id']

            regedate = token_info[0]['regedate']#用户注册时间

            now = datetime.datetime.now()#现在时间

            h_time = datetime.timedelta(seconds=3600*11)#过期时间限制1h

            if now - regedate > h_time:#时间验证是否过期

                return {

                'status': -1,
                'errorCode': 2,
                'errorMsg':'token过期',
                'resultBody': None,
            }

            sql_all = 'select * from company where id = %s'

            company_info = con_obj.select_all(sql_all,(company_id))

            con_obj.close()

            info = {
                'id':company_info[0]['id'],
                'company_name':company_info[0]['id'],
                'product_name':company_info[0]['product_name'],
                'need':company_info[0]['need'],
                'linkman':company_info[0]['linkman'],
                'phone':company_info[0]['phone'],
                'party':company_info[0]['party'],
            }#数据筛选

            return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg':None,
                    'resultBody':{'info':info,'token':token}
                }

    def post_return(self,phone,pass_word):

        con_obj = ConnectMysql()

        company_sql = 'select * from company where phone=%s and pass_word=%s'

        company_info = con_obj.select_all(company_sql,(phone,pass_word))

        if company_info ==None:

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg':'请检查账号密码',
                'resultBody':None
            }

        info = {
                'id':company_info[0]['id'],
                'company_name':company_info[0]['id'],
                'product_name':company_info[0]['product_name'],
                'need':company_info[0]['need'],
                'linkman':company_info[0]['linkman'],
                'phone':company_info[0]['phone'],
                'party':company_info[0]['party'],
            }#数据筛选

        token = uuid.uuid1().int

        company_id = company_info[0]['id']

        token_sql = 'insert into token (token,company_id) values (%s,%s)'

        con_obj.insert_info(token_sql,(token,company_id))

        con_obj.close()

        return {
                    'status': 1,
                    'errorCode': None,
                    'errorMsg':None,
                    'resultBody':{'info':info,'token':token}
                }
    